﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Utils.DC.Web.UI;
using DCSLibrary;

public partial class Admin_Form_Properties_Add : System.Web.UI.Page
{
    #region Cac bien dung chung
    PropertiesUI objProperties = new PropertiesUI();
    DataSet dsProperties;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (txtPropertyID.Value.Trim() != "")
        {
            try
            {
                int result = objProperties.Update(Convert.ToInt32(txtPropertyID.Value), txtPropertyTypeName.Text.Trim(),
                    txtPropertyName.Text.Trim(), txtPropertyValue.Text.Trim(), chkIsEdit.Checked, chkIsActive.Checked);
                if (result > 0)
                {
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                }
                else
                {
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
        else
        {
            try
            {
                int result = objProperties.Add(txtPropertyTypeName.Text.Trim(),
                    txtPropertyName.Text.Trim(), txtPropertyValue.Text.Trim(), chkIsEdit.Checked, chkIsActive.Checked);
                if (result > 0)
                {
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                }
                else
                {
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-chung/quan-tri-thuoc-tinh.aspx");
    }

    private void LoadEditData(int ID)
    {
        DataSet ds = objProperties.GetByID(ID);
        if (ds != null && ds.Tables.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtPropertyID.Value = row["PropertyID"].ToString().Trim();
            txtPropertyName.Text = row["PropertyName"].ToString().Trim();
            txtPropertyTypeName.Text = row["PropertyTypeName"].ToString().Trim();
            txtPropertyValue.Text = row["PropertyValue"].ToString().Trim();
            chkIsActive.Checked = Convert.ToBoolean(row["IsActive"]);
            chkIsEdit.Checked = Convert.ToBoolean(row["IsEdit"]);
        }
    }

    private void ReInsertData()
    {
        //txtPropertyID.Value = "";
        txtPropertyName.Text = "";
        txtPropertyTypeName.Text = "";
        txtPropertyValue.Text = "";
        chkIsActive.Checked = false;
        chkIsEdit.Checked = false;
    }


}
